<?php

global $_W, $_GPC;
$_GPC['do'] = 'slider';
$_GPC['menu'] = 'card';

if ($_GPC['act'] == 'mutdelete') {
    $ids = $_GPC['ids'];
    foreach ($ids as $id) {
        M('slider')->delete($id);
    }
    message('删除成功', referer(), 'success');
}

if ($_GPC['act'] == 'edit') {
    $id = intval($_GPC['id']);
    if ($_W['ispost']) {
        $data = array();
        $data['uniacid'] = $_W['uniacid'];
        if (isset($_GPC['title'])) {
            $data['title'] = trim($_GPC['title']);
        }
        if (!empty($_GPC['image'])) {
            $data['image'] = trim($_GPC['image']);
        }
        if (isset($_GPC['link'])) {
            $data['link'] = trim($_GPC['link']);
        }
        if (isset($_GPC['status'])) {
            $data['status'] = intval($_GPC['status']);
        }
        if (isset($_GPC['type'])) {
            $data['type'] = intval($_GPC['type']);
        }
        if (isset($_GPC['sort'])) {
            $data['sort'] = intval($_GPC['sort']);
        }
        $data['create_time'] = time();
        if (!empty($id)) {
            $data['id'] = $id;
            unset($data['create_time']);
            $question = M('slider')->update($data);
        } else {
            $question = M('slider')->update($data);
        }
        message('保存成功', $this->createWebUrl('slider'), 'success');
    }
    $item = M('slider')->getInfo($id);
    include $this->template('slider_edit');
    exit;
}

if ($_GPC['act'] == 'delete') {
    $id = intval($_GPC['id']);
    if (empty($id)) {
        if ($_W['ispost']) {
            $data = array();
            $data['status'] = 1;
            $data['message'] = '参数错误';
            die(json_encode($data));
        } else {
            message('参数错误', referer(), 'error');
        }
    }
    M('slider')->delete($id);
    if ($_W['ispost']) {
        $data = array();
        $data['status'] = 1;
        $data['message'] = '操作成功';
        die(json_encode($data));
    } else {
        message('删除成功', referer(), 'success');
    }
}

$page = !empty($_GPC['page']) ? intval($_GPC['page']) : 1;
$where = "";
$sliderList = M('slider')->getList($page, $where);
$list = $sliderList['list'];
$typeArr = array(
    1 => '首页',
    2 => '校友',
    3 => '校友圈'
);
foreach ($list as $key => $value) {
    $list[$key]['type'] = $typeArr[$value['type']];
    $list[$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
}

include $this->template('slider');